package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.wemedia.IWeMediaClient;
import com.heima.model.admin.pojos.ApUserRealname;
import com.heima.model.common.dtos.PageRequestDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.mapper.ApUserRealMapper;
import com.heima.user.service.ApUserRealService;
import com.heima.user.service.ApUserService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ApUserRealServiceImpl extends ServiceImpl<ApUserRealMapper, ApUserRealname> implements ApUserRealService {


    /**
     * 分页查询认证列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listApUserReal(PageRequestDto dto) {
        //参数校验
        dto.checkParam();
        //分页查询
        IPage<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());
        //审核状态
        page = page(page, Wrappers.<ApUserRealname>lambdaQuery()
                .eq(dto.getStatus() != null, ApUserRealname::getStatus, dto.getStatus()));
        ResponseResult res = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        res.setData(page.getRecords());
        return res;
    }


    @Autowired
    private IWeMediaClient weMediaAuthorClient;
    @Autowired
    private ApUserService apUserService;
    /**
     * 审核通过
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult authPass(String id) {
        ApUserRealname author = getById(Integer.valueOf(id));
        author.setStatus((short) 9);
        updateById(author);
        //开通自媒体端账号（该账号的用户名和密码与app一致）
        ApUser apUser = apUserService.getById(id);
        weMediaAuthorClient.saveAuthor(apUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 审核失败
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult authFail(String id, String msg) {
        ApUserRealname author = getById(Integer.valueOf(id));
        author.setStatus((short) 2);
        author.setReason(msg);
        updateById(author);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
